﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using ZLCMS.Data.Common;
using NHibernate;
using ZLCMS.Business;
using ZLCMS.Business.Commands.SessionCommands;
using System.Web.Security;
using ZLCMS.Business.Util;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Shop.Commands.Member;
using DotNet.Common.Command;

namespace ZLCMS.Web.MemberUser
{
    public partial class RegEmail : System.Web.UI.Page
    {
            public string siteName = "";
        protected void Page_Load(object sender, EventArgs e)
        {
             siteName =  ParamKit.GetValue("SystemConfig", "SiteName", "广州臻蓝网络");
            if (Session[GlobalSessionString.CurrentMemberAccount] != null)
            {
                Response.Redirect("/MemberUser/");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Forms身份验证初始化 
            FormsAuthentication.Initialize();


            String md5Code = FormsAuthentication.HashPasswordForStoringInConfigFile(tb_pwd1.Text.Trim(), "MD5").ToLower();
            ClientScriptManager csm = this.Page.ClientScript;

            if (Session[GlobalSessionString.UserCheckCode] == null)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('验证码过期，请重试。", true); 
                return;
            }

            if (Session[GlobalSessionString.UserCheckCode].ToString() != tb_cc.Text.Trim())
            {
                  csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('验证码输入错误，请重试。", true); 
                return;
            }

            try
            {
                SignupParameter cmdParam = new SignupParameter();
                cmdParam.Email = tb_id.Text;
                cmdParam.Password = md5Code;
                cmdParam.Birthday = DateTime.Parse("1970-1-1");
                cmdParam.LastLog = DateTime.Now;
                cmdParam.RegType = "email";
                //cmdParam.Status = "0";

                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    ICommandResult<SignupResult> cmdResult = new SignupCommand().Execute(session, cmdParam);
                    if (cmdResult.Error)
                    {
                        throw new Exception(cmdResult.ErrorMsg);
                    }
                    session.Flush();
                }
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('感谢注册！确认邮件已发送至你的注册邮箱 : " + cmdParam.Email + "。请进入邮箱查看邮件，并激活帐号.');location.href='/MemberUser/Login.aspx'", true);
            }
            catch (System.Exception ex)
            {
                csm.RegisterStartupScript(this.GetType(), "error_tip", "alert('" + ex.Message + "');", true);
                return;
            }
        }
    }
}